package com.shanshan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanshan.system.easyexcel.DiseaseExcel;
import com.shanshan.system.entity.Admin;
import com.shanshan.system.entity.Branch;
import com.shanshan.system.entity.Permission;
import com.shanshan.system.entity.Record;
import com.shanshan.system.mapper.AdminMapper;
import com.shanshan.system.mapper.BranchMapper;
import com.shanshan.system.mapper.PermissionMapper;
import com.shanshan.system.mapper.RecordMapper;
import com.shanshan.system.service.RecordService;
import com.shanshan.system.vo.RecordContent;
import com.shanshan.system.vo.Result;
import com.shanshan.system.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author keluoyisi
 * @program: shanshan
 * @description:
 * @date 2023-02-14 13:38:19
 */
@Service
@SuppressWarnings("all")
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BranchMapper branchMapper;
    @Override
    public Result addRecord(RecordContent recordContent) {
        Record record = new Record();
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("account",recordContent.getAccount());
        Admin admin = adminMapper.selectOne(wrapper);
        record.setAdminId(admin.getId());
        record.setTitle(recordContent.getTitle());
        record.setName(recordContent.getName());
        record.setSort(recordContent.getSort());
        record.setContent(recordContent.getContent());
        record.setCreatetime(LocalDateTime.now());
        String s = randomUUID();
        String substring = s.substring(0, 15);
        record.setUuid(substring);
        int insert = recordMapper.insert(record);
        if (insert==1){
            return new Result(200,"成功");

        }
        return new Result(500,"失败");
    }

    @Override
    public Result selectAll(UserVo userVo) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("account",userVo.getAccount());
        Admin admin1 = adminMapper.selectOne(wrapper);
        QueryWrapper<Record> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("admin_id",admin1.getId());
        List<Record> records = recordMapper.selectList(wrapper1);
        for (Record record : records) {
            Admin admin = adminMapper.selectById(record.getAdminId());
            Branch branch = branchMapper.selectById(admin.getBranchId());
            record.setDept(branch.getName());
        }
        return new Result(200,"成功",records);
    }

    @Override
    public DiseaseExcel recordService(int reservationId) {
        Record record = recordMapper.selectById(reservationId);

        DiseaseExcel diseaseExcel = new DiseaseExcel();
        diseaseExcel.setUuid(record.getUuid());
        diseaseExcel.setContent(record.getContent());
        diseaseExcel.setName(record.getName());
        Admin admin = adminMapper.selectById(record.getAdminId());
        Branch branch = branchMapper.selectById(admin.getBranchId());
        diseaseExcel.setDept(branch.getName());
        diseaseExcel.setSort(record.getSort());
        diseaseExcel.setTitle(record.getTitle());
        return diseaseExcel;
    }

    public static String randomUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "").toUpperCase();
    }
}
